/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.sind.blip;

import pl.sind.blip.entities.Error;
import pl.sind.http.HttpRequestException;

/**
 * Exception thrown in case of blip API execution errors.<br/>
 * Signals errors in library due to API changes or Blip server errors or
 * conectivity problems. See also: {@link BlipUnauthorizedException}
 * {@link BlipNotFoundException}
 * 
 * @author Lukasz Wozniak
 * 
 */
public class BlipConnectorException extends Exception {
	private static final long serialVersionUID = 2194694408167446236L;
	private int code;
	private Error error;

	/**
	 * 
	 */
	public BlipConnectorException() {
	}

	/**
	 * @param message
	 */
	public BlipConnectorException(String message) {
		super(message);
	}

	/**
	 * @param message
	 */
	public BlipConnectorException(int code, String message) {
		super(message);
		this.code = code;
	}

	public BlipConnectorException(int code, String message, Error error) {
		super(message);
		this.code = code;
		this.error = error;
	}

	/**
	 * @param cause
	 */
	public BlipConnectorException(Throwable cause) {
		super(cause);
		if (cause instanceof HttpRequestException) {
			this.code = ((HttpRequestException) cause).getCode();
		}
	}

	/**
	 * @param message
	 * @param cause
	 */
	public BlipConnectorException(String message, Throwable cause) {
		super(message, cause);
		if (cause instanceof HttpRequestException) {
			this.code = ((HttpRequestException) cause).getCode();
		}
	}

	public int getCode() {
		return code;
	}

	/**
	 * If Blip provided Error JSON message in its response it is available here.
	 * 
	 * @return {@link Error} or <code>null</code> if not available
	 */
	public Error getError() {
		return error;
	}

}
